﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace Future.FormBase
{
    /// <summary>
    /// 系统工具类
    /// </summary>
    public class MdiTools
    {
        /// <summary>
        /// 打开IE浏览器或打开Outlook
        /// </summary>
        /// <param name="url"></param>
        public static void OpenIE(string url)
        {
            System.Diagnostics.Process.Start(url);
        }

        public static Form OpenChildForm(IMdiForm mdi, Type formType, ToolStripMenuItem sender)
        {
            try
            {
                Common.CCursor.ShowWaitCursor();
                Form child = ActiveChildForm(mdi as Form, formType.ToString());
                if (child != null)
                {
                    child.Activate();
                    return child;
                }

                Form form = (Form)formType.Assembly.CreateInstance(formType.FullName);
                if (form != null && form is Form)
                {
                    child = form as Form;

                    if (form is IMdiChildForm)
                    {
                        (child as IMdiChildForm).ToolbarRegister = mdi.MdiToolbar;
                        (child as IMdiChildForm).RegisterObserver(mdi.MdiObservers);
                        child.MdiParent = (Form)mdi;
                    }
                    child.Show();
                    return child;
                }
                else
                    throw new Exception("未知的类型窗体!");
            }
            finally
            {
                Common.CCursor.ShowDefaultCursor();
            }
        }

        public static Form ActiveChildForm(Form mdi, string formTypeName)
        {
            for (int i = 0; i < mdi.MdiChildren.Length; i++)
            {
                string ss = mdi.MdiChildren[i].GetType().ToString().ToUpper();
                if (ss == formTypeName.ToUpper())
                {
                    mdi.MdiChildren[i].Activate();
                    return mdi.MdiChildren[i];
                }
            }
            return null;
        }
    }
}
